package com.yunchang.springboot.scheduled.mwsfinance;

import com.yunchang.springboot.base.BaseService;
import com.yunchang.springboot.mapper.caravan.CaravanMskuDataMapper;
import com.yunchang.springboot.mapper.mwsfinance.FInventoryDailyMapper;
import com.yunchang.springboot.mapper.mwsfinance.FRemovalFeeMapper;
import com.yunchang.springboot.mapper.mwsfinance.FRemovalMapper;
import com.yunchang.springboot.mapper.mwsreport.MwsDailyInventoryHistoryMapper;
import com.yunchang.springboot.mapper.mwsreport.MwsRemovalOrderDetailMapper;
import com.yunchang.springboot.mapper.mwsreport.MwsRemovalShipmentDetailMapper;
import com.yunchang.springboot.mapper.mwsutils.CwfzFinancialSubjectsMapper;
import com.yunchang.springboot.mapper.mwsutils.CwfzRemovalShipmentTypeMapper;
import com.yunchang.springboot.models.mwsfinance.FRemoval;
import com.yunchang.springboot.models.mwsfinance.FRemovalFee;
import com.yunchang.springboot.models.mwsreportbean.MwsRemovalShipmentDetail;
import com.yunchang.springboot.utils.DateUtil;
import com.yunchang.springboot.utils.UUIDUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.*;

/**
 * 移除货件
 *
 * @author czw
 */
@Component
@Configuration
@Service
@Slf4j
@EnableScheduling
public class FRemovalScheduled extends BaseService {

    private String SELL_ABLE = "SELLABLE";

    private String ORDER_STATUS_COMPLETE = "Completed";

    private String ORDER_STATUS_PENDING = "Pending";

    private List<String> EUROPE_COUNTRY_NAME = Arrays.asList("DE", "UK", "FR", "IT", "ES");

    static final String JP = "JP";

    static final String AREA = "area";

    static final String EU = "EU";

    static final String DONATION = "Donation";

    static final String REMOVAL_TYPE = "移除至美东海外仓2";

    /**
     * 借方科目
     */
    private static final String DEBIT_SUBJECT_ID = "660115";

    private static final String DEBIT_SUBJECT_ID_OTHER = "660116";

    /**
     * 贷方科目
     */
    private static final String CREDIT_SUBJECT_ID = "220207";

    private static final String CREDIT_SUBJECT_ID_OTHER = "220204";

    static Integer LIMIT_VALUE = 10000;

    static String DOCUMENTS_ID = "YCCKD";

    static String DOCUMENTS_ID_PUSH = "YCFYD";

    /**
     * 出入库明细
     */
    @Autowired
    FInventoryDetailsScheduled fInventoryDetailsScheduled;

    /**
     * 移除货件原始数据
     */
    private MwsRemovalShipmentDetailMapper mwsRemovalShipmentDetailMapper;

    @Autowired
    public void setMwsRemovalShipmentDetailMapper(MwsRemovalShipmentDetailMapper mwsRemovalShipmentDetailMapper) {
        this.mwsRemovalShipmentDetailMapper = mwsRemovalShipmentDetailMapper;
    }

    /**
     * 每日库存记录原始数据
     */
    private MwsDailyInventoryHistoryMapper mwsDailyInventoryHistoryMapper;

    @Autowired
    public void setMwsDailyInventoryHistoryMapper(MwsDailyInventoryHistoryMapper mwsDailyInventoryHistoryMapper) {
        this.mwsDailyInventoryHistoryMapper = mwsDailyInventoryHistoryMapper;
    }

    /**
     * 移除订单原始数据
     */
    private MwsRemovalOrderDetailMapper mwsRemovalOrderDetailMapper;

    @Autowired
    public void setMwsRemovalOrderDetailMapper(MwsRemovalOrderDetailMapper mwsRemovalOrderDetailMapper) {
        this.mwsRemovalOrderDetailMapper = mwsRemovalOrderDetailMapper;
    }

    /**
     * 获取removal_type
     */
    private CwfzRemovalShipmentTypeMapper cwfzRemovalShipmentTypeMapper;

    @Autowired
    public void setCwfzRemovalShipmentTypeMapper(CwfzRemovalShipmentTypeMapper cwfzRemovalShipmentTypeMapper) {
        this.cwfzRemovalShipmentTypeMapper = cwfzRemovalShipmentTypeMapper;
    }

    /**
     * 获取借贷的科目
     */
    private CwfzFinancialSubjectsMapper cwfzFinancialSubjectsMapper;

    @Autowired
    public void setCwfzFinancialSubjectsMapper(CwfzFinancialSubjectsMapper cwfzFinancialSubjectsMapper) {
        this.cwfzFinancialSubjectsMapper = cwfzFinancialSubjectsMapper;
    }

    /**
     * 移除货件财务数据
     */
    private FRemovalMapper fRemovalMapper;

    @Autowired
    public void setFRemovalMapper(FRemovalMapper fRemovalMapper) {
        this.fRemovalMapper = fRemovalMapper;
    }


    /**
     * 移除货件下推数据
     */
    private FRemovalFeeMapper fRemovalFeeMapper;

    @Autowired
    public void setfRemovalFeeMapper(FRemovalFeeMapper fRemovalFeeMapper) {
        this.fRemovalFeeMapper = fRemovalFeeMapper;
    }

    /**
     * 库存数据
     */
    private FInventoryDailyMapper fInventoryDailyMapper;

    @Autowired
    public void setFInventoryDailyMapper(FInventoryDailyMapper fInventoryDailyMapper) {
        this.fInventoryDailyMapper = fInventoryDailyMapper;
    }

    /**
     * 获取stockSku
     */
    private CaravanMskuDataMapper caravanMskuDataMapper;

    @Autowired
    public void setCaravanMskuDataMapper(CaravanMskuDataMapper caravanMskuDataMapper) {
        this.caravanMskuDataMapper = caravanMskuDataMapper;
    }


    public void getFRemoval(String date) {
        List<MwsRemovalShipmentDetail> mwsRemovalShipmentDetailList = mwsRemovalShipmentDetailMapper.selectByFinanceStatus(1, null, date);
        Integer mwsRemovalShipmentDetailListSize = mwsRemovalShipmentDetailList.size();
        log.info("移除货件，原始数据的数量为：" + mwsRemovalShipmentDetailListSize);
        int forNum = 1;
        if (mwsRemovalShipmentDetailListSize > LIMIT_VALUE) {
            forNum += mwsRemovalShipmentDetailListSize / LIMIT_VALUE;
            for (; forNum > 0; forNum--) {
                log.info("移除货件生成财务数据剩余次数：" + forNum);
                mwsRemovalShipmentDetailList = mwsRemovalShipmentDetailMapper.selectByFinanceStatus(1, LIMIT_VALUE, date);
                mwsRemovalShipmentDetailDispose(mwsRemovalShipmentDetailList);
            }
        } else {
            mwsRemovalShipmentDetailDispose(mwsRemovalShipmentDetailList);
        }
    }

    public void mwsRemovalShipmentDetailDispose(List<MwsRemovalShipmentDetail> mwsRemovalShipmentDetailList) {
        if (null != mwsRemovalShipmentDetailList && !mwsRemovalShipmentDetailList.isEmpty()) {
            List<Object> fRemovalInsertList = new ArrayList<>();
            for (MwsRemovalShipmentDetail mwsRemovalShipmentDetail : mwsRemovalShipmentDetailList) {
                FRemoval fRemoval = new FRemoval();
                fRemoval.setIsManualAdjustment(1);
                fRemoval.setApprovalStatus(1);
                fRemoval.setIsGenerateRemovalFee(1);
                fRemoval.setIsInventoryDetails(1);
                fRemoval.setStockSkuStatus(1);
                fRemoval.setRemovalStatus(1);
                fRemoval.setCostUnitStatus(1);
                fRemoval.setDocumentsId(DOCUMENTS_ID + UUIDUtil.getUUID());
                String requestDate = mwsRemovalShipmentDetail.getRequestDate();
                fRemoval.setRequestDate(requestDate);
                String shipmentDate = mwsRemovalShipmentDetail.getShipmentDate();
                fRemoval.setShipmentDate(shipmentDate);
                String sellerId = mwsRemovalShipmentDetail.getSellerId();
                String sku = mwsRemovalShipmentDetail.getSku();
                String area = mwsRemovalShipmentDetail.getArea();
                String fnSku = mwsRemovalShipmentDetail.getFnsku();
                fRemoval.setSellerId(sellerId);
                String country = getCountry(area, sellerId, fnSku, sku);
                if (country.equals(AREA)) {
                    fRemoval.setIsManualAdjustment(2);
                    fRemoval.setCountry(area);
                } else {
                    fRemoval.setCountry(country);
                }
                String orderId = mwsRemovalShipmentDetail.getOrderId();
                fRemoval.setOrderId(orderId);
                fRemoval.setSku(mwsRemovalShipmentDetail.getSku());
                fRemoval.setFnsku(fnSku);
                String disposition = mwsRemovalShipmentDetail.getDisposition();
                disposition = disposition.toUpperCase();
                if (disposition.equals(SELL_ABLE)) {
                    fRemoval.setDisposition(1);
                } else {
                    fRemoval.setDisposition(0);
                }
                fRemoval.setOrderStatus(getOrderStatusBySellerIdAndOrderIdAndSku(sellerId, orderId, sku));
                String trackingNumber = mwsRemovalShipmentDetail.getTrackingNumber();
                if (getRemovalTypeByTrackingNumber(trackingNumber, orderId, sku, area).equals(REMOVAL_TYPE)) {
                    fRemoval.setRemovalStatus(2);
                    fRemoval.setRemovalType("移除至美东海外仓");
                } else {
                    fRemoval.setRemovalType(getRemovalTypeByTrackingNumber(trackingNumber, orderId, sku, area));
                }
                if (1 == fRemoval.getDisposition()) {
                    fRemoval.setType("可售商品" + fRemoval.getRemovalType());
                }
                if (0 == fRemoval.getDisposition()) {
                    fRemoval.setType("不可售商品" + fRemoval.getRemovalType());
                }
                Integer quantity = mwsRemovalShipmentDetail.getShippedQuantity();
                fRemoval.setQuantity(quantity);
                fRemoval.setDebitSubjectId(getDebitSubjectId(fRemoval.getType()));
                fRemoval.setCreditSubjectId(getCreditSubjectId(fRemoval.getType()));
                Double costUnit;
                Map<String, Double> costUnitMap = getCostUnit(sellerId, fRemoval.getCountry(), fRemoval.getCreditSubjectId(), shipmentDate, sku);
                if (null != costUnitMap && !costUnitMap.isEmpty()) {
                    if (costUnitMap.containsKey("costUnit2")) {
                        fRemoval.setCostUnitStatus(2);
                        costUnit = costUnitMap.get("costUnit2");
                    } else {
                        costUnit = costUnitMap.get("costUnit1");
                    }
                    fRemoval.setCostUnit(costUnit);
                    fRemoval.setAmount(new BigDecimal(quantity * costUnit).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue());
                }
                fRemoval.setCurrency("RMB");
                Map<String,String> stockSkuMap = getStockSku(sku,country,sellerId);
                if(null != stockSkuMap && !stockSkuMap.isEmpty()){
                    if(stockSkuMap.containsKey("stockSku")){
                        fRemoval.setStockSku(stockSkuMap.get("stockSku"));
                    }
                    if(stockSkuMap.containsKey("stockSku2")){
                        fRemoval.setStockSku(stockSkuMap.get("stockSku2"));
                        fRemoval.setStockSkuStatus(2);
                    }
                    if(stockSkuMap.containsKey("stockSku3")){
                        fRemoval.setStockSku(stockSkuMap.get("stockSku3"));
                        fRemoval.setStockSkuStatus(3);
                    }
                    if(stockSkuMap.containsKey("stockSku4")){
                        fRemoval.setStockSku(stockSkuMap.get("stockSku4"));
                        fRemoval.setStockSkuStatus(4);
                    }
                }
                fRemoval.setCreateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                fRemoval.setUpdateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                fRemovalInsertList.add(fRemoval);
                mwsRemovalShipmentDetail.setFinanceStatus(2);
            }
            try {
                baseInsertList(fRemovalInsertList);
                mwsRemovalShipmentDetailMapper.updateFinanceStatusByMwsRemovalShipmentDetailList(mwsRemovalShipmentDetailList);
                log.info("移除货件财务数据已生成");
            } catch (Exception e) {
                log.error("插入数据时出现错误，移除货件" + e);
                throw new RuntimeException("插入数据时出现错误，移除货件");
            }
        } else {
            log.info("未查询到对应的移除货件数据列表，时间为" + DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
        }
        // 更新数据移除货件财务数据的状态
        updateFRemoval();
    }

    /**
     * 更新数据的orderStatus
     */
    public void updateFRemoval() {
        List<FRemoval> fRemovalList = fRemovalMapper.selectByIsGenerateRemovalFee(1);
        log.info("原始数据大小为：" + fRemovalList.size());
        int updateNum = 0;
        if (null != fRemovalList && !fRemovalList.isEmpty()) {
            // 对orderStatus状态为未完成的数据进行更新
            for (FRemoval fRemoval : fRemovalList) {
                FRemoval fRemovalTemp = new FRemoval();
                String sellerId = fRemoval.getSellerId();
                String orderId = fRemoval.getOrderId();
                String sku = fRemoval.getSku();
                String orderStatus = getOrderStatusBySellerIdAndOrderIdAndSku(sellerId, orderId, sku);
                String nowOrderStatus = fRemoval.getOrderStatus();
                if (!StringUtils.isEmpty(orderStatus) && !StringUtils.isEmpty(nowOrderStatus)) {
                    if (nowOrderStatus.equals(orderStatus)) {
                        continue;
                    } else {
                        fRemovalTemp.setOrderStatus(orderStatus);
                        updateNum++;
                    }
                }
            }
            if (updateNum != 0) {
                fRemovalMapper.updateIsGenerateRemovalFeeByFRemovalList(fRemovalList);
                log.info("移除货件，更新状态已更新");
            } else {
                log.info("移除货件，没有需要更新的数据");
            }
        }
        // 生成移除货件的下推数据
        getFRemovalFee();
    }

    /**
     * 生成移除货件的下推数据
     */
    public void getFRemovalFee() {
        List<FRemoval> fRemovalList = fRemovalMapper.selectByOrderStatusAndIsGenerateRemovalFee(ORDER_STATUS_COMPLETE, 1);
        if (null != fRemovalList && !fRemovalList.isEmpty()) {
            List<Object> fRemovalFeeInsertList = new ArrayList<>();
            for (FRemoval fRemoval : fRemovalList) {
                FRemovalFee fRemovalFee = new FRemovalFee();
                fRemovalFee.setDocumentsId(DOCUMENTS_ID_PUSH + UUIDUtil.getUUID());
                String sellerId = fRemoval.getSellerId();
                fRemovalFee.setSellerId(sellerId);
                String country = fRemoval.getCountry();
                fRemovalFee.setCountry(country);
                fRemovalFee.setShipmentDate(fRemoval.getShipmentDate());
                String orderId = fRemoval.getOrderId();
                String sku = fRemoval.getSku();
                fRemovalFee.setLastUpdateDate(getLastUpdateDateBySellerIdAndOrderIdAndSku(sellerId, orderId, sku));
                fRemovalFee.setOrderId(orderId);
                fRemovalFee.setSku(sku);
                fRemovalFee.setFnsku(fRemoval.getFnsku());
                Integer quantity = fRemoval.getQuantity();
                fRemovalFee.setQuantity(quantity);
                String removalType = fRemoval.getRemovalType();
                fRemovalFee.setFeeUnit(getFeeUnitByRemovalType(removalType, sellerId, orderId, sku));
                Double amount = fRemoval.getAmount();
                if (null != amount) {
                    fRemovalFee.setAmount(new BigDecimal(amount * quantity).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue());
                }
                fRemovalFee.setCurrency(getCurrencyBySellerIdAndOrderIdAndSku(sellerId, orderId, sku));
                if (removalType.equals("弃置")) {
                    fRemovalFee.setDebitSubjectId(DEBIT_SUBJECT_ID);
                    fRemovalFee.setCreditSubjectId(CREDIT_SUBJECT_ID);
                } else {
                    fRemovalFee.setDebitSubjectId(DEBIT_SUBJECT_ID_OTHER);
                    fRemovalFee.setCreditSubjectId(CREDIT_SUBJECT_ID_OTHER);
                }
                fRemovalFee.setCreateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                fRemovalFee.setUpdateTime(DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
                fRemovalFeeInsertList.add(fRemovalFee);
                fRemoval.setIsGenerateRemovalFee(2);
            }
            try {
                baseInsertList(fRemovalFeeInsertList);
                fRemovalMapper.updateIsGenerateRemovalFeeByFRemovalList(fRemovalList);
                log.info("移除货件下推数据已生成");
            } catch (Exception e) {
                log.error("插入数据时出现错误，移除货件下推数据" + e);
                throw new RuntimeException("插入数据时出现错误，移除货件下推数据");
            }
        } else {
            log.info("未查询到对应的移除货件下推数据列表，时间为" + DateUtil.getTodayDate("yyyy-MM-dd HH:mm:ss"));
        }
    }

    /**
     * 根据requestDate、sellerId、sku获取country
     *
     * @param requestDate
     * @param sellerId
     * @param sku
     */
    public String getCountryByRequestDateAndSellerIdAndSku(String requestDate, String sellerId, String sku) throws ParseException {
        if (StringUtils.isEmpty(requestDate) || StringUtils.isEmpty(sellerId) || StringUtils.isEmpty(sku)) {
            log.info("根据requestDate、sellerId、sku获取country时出现requestDate、sellerId、sku三者中有为空的情况，移除货件");
            return "";
        }
        String requestDateStart;
        String requestDateEnd = requestDate.substring(0, 10);
        requestDateStart = DateUtil.dateToFutureDate(requestDateEnd, -60);
        List<String> countryList = mwsDailyInventoryHistoryMapper.getCountryByRequestDateAndSellerIdAndSku(requestDateStart, requestDateEnd, sellerId, sku);
        if (null != countryList && !countryList.isEmpty()) {
            if (countryList.size() == 1) {
                return countryList.get(0);
            }
        }
        return "";
    }


    /**
     * 根据区域和fulfillmentCenterId获取国家名称
     *
     * @param area
     * @param sellerId
     * @return
     */
    public String getCountry(String area, String sellerId, String fnSku, String sku) {
        if (area.equals(JP)) {
            return JP;
        }
        if (EUROPE_COUNTRY_NAME.contains(area)) {
            return "EU";
        }
        if (!StringUtils.isEmpty(sellerId) && StringUtils.isEmpty(fnSku)) {
            List<String> countryList = mwsDailyInventoryHistoryMapper.selectCountryBySellerIdAndFnSku(sellerId, fnSku);
            if (null != countryList && countryList.size() == 1) {
                return countryList.get(0);
            }
        }
        if (!StringUtils.isEmpty(sellerId) && !StringUtils.isEmpty(sku)) {
            List<String> countryList = mwsDailyInventoryHistoryMapper.selectCountryBySellerIdAndSku(sellerId, sku);
            if (null != countryList && countryList.size() == 1) {
                return countryList.get(0);
            } else {
                return AREA;
            }
        }
        return AREA;
    }

    /**
     * 根据sellerId和orderId以及sku获取orderStatus
     *
     * @param sellerId
     * @param orderId
     * @param sku
     * @return
     */
    public String getOrderStatusBySellerIdAndOrderIdAndSku(String sellerId, String orderId, String sku) {
        if (StringUtils.isEmpty(sellerId) || StringUtils.isEmpty(orderId) || StringUtils.isEmpty(sku)) {
            log.info("根据sellerId和orderId以及sku获取orderStatus,sellerId或orderId或sku三者中有为空的情况，移除货件");
            return "";
        }
        return mwsRemovalOrderDetailMapper.selectOrderStatus(sellerId, orderId, sku);
    }

    /**
     * 根据trackingNumber和 orderId 获取removalType
     *
     * @param trackingNumber
     * @param orderId
     * @param sku
     * @return 若是无法判断的则其值为移除至美东海外仓2
     */
    public String getRemovalTypeByTrackingNumber(String trackingNumber, String orderId, String sku, String area) {
        String W = "W";
        String E = "E";
        String removalType;
        if (trackingNumber.equals(DONATION)) {
            return "弃置";
        } else if (EUROPE_COUNTRY_NAME.contains(area)) {
            return "移除至欧洲海外仓";
        } else if (area.equals(JP)) {
            return "移除至日本海外仓";
        } else if (orderId.startsWith(W)) {
            return "移除至美东海外仓";
        } else if (orderId.startsWith(E)) {
            return "移除至美西海外仓";
        } else {
            removalType = cwfzRemovalShipmentTypeMapper.selectRemovalTypeByoOrderIdAndSku(orderId, sku);
            if (StringUtils.isEmpty(removalType)) {
                return "移除至美东海外仓2";
            }
            return removalType;
        }
    }

    /**
     * 根据type获取借方科目
     *
     * @param type
     * @return
     */
    public String getDebitSubjectId(String type) {
        if (StringUtils.isEmpty(type)) {
            return "";
        }
        return cwfzFinancialSubjectsMapper.selectDebitSubjectIdByType(type);
    }


    /**
     * 根据type获取贷方科目
     *
     * @param type
     * @return
     */
    public String getCreditSubjectId(String type) {
        if (StringUtils.isEmpty(type)) {
            return "";
        }
        return cwfzFinancialSubjectsMapper.selectCreditSubjectIdByType(type);
    }

    /**
     * 根据sellerId 和 orderId 以及 sku 获取lastUpdateDate
     *
     * @param sellerId
     * @param orderId
     * @param sku
     * @return
     */
    public String getLastUpdateDateBySellerIdAndOrderIdAndSku(String sellerId, String orderId, String sku) {
        if (StringUtils.isEmpty(sellerId) && StringUtils.isEmpty(orderId) && StringUtils.isEmpty(sku)) {
            return "";
        }
        return mwsRemovalOrderDetailMapper.selectLastUpdateDateBySellerIdAndOrderIdAndSku(sellerId, orderId, sku);
    }

    /**
     * 根据removalType sellerId orderId sku 获取 fee_unit
     *
     * @param removalType
     * @param sellerId
     * @param orderId
     * @param sku
     * @return
     */
    public Double getFeeUnitByRemovalType(String removalType, String sellerId, String orderId, String sku) {
        Double feeUnit = 0.0;
        if (StringUtils.isEmpty(removalType)) {
            return feeUnit;
        }
        if (removalType.equals("弃置")) {
            Double removalFee = mwsRemovalOrderDetailMapper.selectRemovalFeeBySellerIdAndOrderIdAndSkuAndOrderType(sellerId, orderId, sku, "Disposal");
            Integer disposedQuantity = mwsRemovalOrderDetailMapper.selectDisposedQuantityBySellerIdAndOrderIdAndSkuAndOrderType(sellerId, orderId, sku, "Disposal");
            if (null != removalFee && removalFee != 0.0 && null != disposedQuantity && disposedQuantity != 0) {
                feeUnit = new BigDecimal(removalFee / disposedQuantity).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
            }
        } else {
            Double removalFee = mwsRemovalOrderDetailMapper.selectRemovalFeeBySellerIdAndOrderIdAndSkuAndOrderType(sellerId, orderId, sku, "Return");
            Integer shippedQuantity = mwsRemovalOrderDetailMapper.selectShippedQuantityBySellerIdAndOrderIdAndSkuAndOrderType(sellerId, orderId, sku, "Return");
            if (null != removalFee && removalFee != 0.0 && null != shippedQuantity && shippedQuantity != 0) {
                feeUnit = new BigDecimal(removalFee / shippedQuantity).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue();
            }
        }
        return feeUnit;
    }

    /**
     * 根据sellerId 和 orderId 以及 sku 获取Currency
     *
     * @param sellerId
     * @param orderId
     * @param sku
     * @return
     */
    public String getCurrencyBySellerIdAndOrderIdAndSku(String sellerId, String orderId, String sku) {
        if (StringUtils.isEmpty(sellerId) && StringUtils.isEmpty(orderId) && StringUtils.isEmpty(sku)) {
            return "";
        }
        return mwsRemovalOrderDetailMapper.selectCurrency(sellerId, orderId, sku);
    }

    /**
     * 获取 costUnit 两种方式
     *
     * @param sellerId
     * @param country
     * @param creditSubjectId
     * @param shipmentDate
     * @param sku
     * @return 第一种方案的key值为costUnit1 第二种方案的key值为 costUnit2
     */
    public Map<String, Double> getCostUnit(String sellerId, String country, String creditSubjectId, String shipmentDate, String sku) {
        if (!StringUtils.isEmpty(sellerId) && !StringUtils.isEmpty(country) && !StringUtils.isEmpty(creditSubjectId) && !StringUtils.isEmpty(shipmentDate) && !StringUtils.isEmpty(sku)) {
            Double amount = fInventoryDailyMapper.selectSumAmountBySellerIdAndCountryAndSkuAndCreditSubjectIdAndDate(sellerId, country, sku, creditSubjectId, shipmentDate.substring(0,10));
            Integer quantity = fInventoryDailyMapper.selectSumQuantityBySellerIdAndCountryAndSkuAndCreditSubjectIdAndDate(sellerId, country, sku, creditSubjectId, shipmentDate.substring(0,10));
            if (null != amount && null != quantity && amount != 0.0 && quantity != 0) {
                return new HashMap<String, Double>(1) {{
                    put("costUnit1", new BigDecimal(amount / quantity).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue());
                }};
            }
            Map<String, Object> amountAndQuantityMap = fInventoryDailyMapper.selectSumAmountAndSumQuantityBySellerIdAndCountryAndSkuAndWarehouse(sellerId, country, sku, creditSubjectId);
            if (null != amountAndQuantityMap && !amountAndQuantityMap.isEmpty()) {
                if (null != amountAndQuantityMap.get("amount") && null != amountAndQuantityMap.get("quantity")) {
                    Double amount2 = new Double(amountAndQuantityMap.get("amount").toString());
                    Integer quantity2 = new Integer(amountAndQuantityMap.get("quantity").toString());
                    if (null != amount2 && null != quantity2 && amount2 != 0 && quantity2 != 0) {
                        return new HashMap<String, Double>(1) {{
                            put("costUnit2", new BigDecimal(amount2 / quantity2).setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue());
                        }};
                    }
                }
            }
        }
        log.info("getCostUnit 获取costUnit为空，移除货件财务数据。sellerId：" + sellerId + " country：" + country + " sku：" + sku + " creditSubjectId：" + creditSubjectId + " shipmentDate:" + shipmentDate);
        return null;
    }

    /**
     * 获取stockSku
     *
     * @param sku
     * @param country
     * @param sellerId
     * @return
     */
    public Map<String, String> getStockSku(String sku, String country, String sellerId) {
        if (!StringUtils.isEmpty(country)) {
            if (country.equals(EU)) {
                country = "o5";
            } else {
                country = country.toLowerCase();
            }
            List<String> stockSkuList = caravanMskuDataMapper.selectStockSkuBySkuAndAmazonSite(sku, country);
            if (null != stockSkuList && !stockSkuList.isEmpty()) {
                if (stockSkuList.size() == 1) {
                    return new HashMap<String, String>(1) {{
                        put("stockSku", stockSkuList.get(0));
                    }};
                }
            }
            String stockSku = caravanMskuDataMapper.selectStockSkuBySellerIdAndCountryAndSku(sellerId, country, sku);
            if (!StringUtils.isEmpty(stockSku)) {
                return new HashMap<String, String>(1) {{
                    put("stockSku2", stockSku);
                }};
            }
            String  stockSkuByMaxDate = caravanMskuDataMapper.selectStockSkuBySkuAndAmazonSiteAndDateMax(sku,country);
            if(!StringUtils.isEmpty(stockSkuByMaxDate)){
                return new HashMap<String, String>(1) {{
                    put("stockSku3", stockSkuByMaxDate);
                }};
            }
        }
        log.info("获取stockSku时使stockSku = sku,移除货件，sku: "+sku+" country: "+country+" sellerId:"+sellerId);
        return new HashMap<String, String>(1) {{
            put("stockSku4", sku);
        }};
    }
}
